package org.bigjava.user.web.controller;

import org.bigjava.bean.*;
import org.bigjava.user.service.impl.UserFunctionServiceImpl;
import org.bigjava.user.service.impl.UserServicesImpl;
import org.bigjava.user.util.AESUtil;
import org.bigjava.user.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.Http2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/userfunction/")
public class UserFunctionController {
    @Autowired
    UserFunctionServiceImpl userFunctionService;

    @Autowired
    UserServicesImpl services;

    AESUtil aesUtil = new AESUtil();

    DateUtil dateUtil = new DateUtil();

    @RequestMapping("addattention")
    public void addAttention(@RequestParam("topicId")long topicId, HttpSession session){
        //用户关注
        String userId = (String) session.getAttribute("userId");
        userFunctionService.addAttention(topicId, userId);
    }

    @RequestMapping("cancelattention")
    public void cancelAttention(@RequestParam("topicId")long topicId,
                                HttpSession session){
        //用户取消关注
        String id = (String)session.getAttribute("userId");
        userFunctionService.cancelAttention(topicId, id);
    }

    @RequestMapping("queryattentiontopic")
    public void queryAttentionTopic(@RequestParam("topicId")long topicId){
        //查询话题粉丝数 ok
       long number = userFunctionService.queryAttentionTopic(topicId);
       System.out.println(number);
    }

    @RequestMapping("updatepetname")
    public void updatePetName(HttpSession session,@RequestParam("petName")String petName){
        //修改昵称 ok
        String id = (String)session.getAttribute("userId");
        userFunctionService.updatePetName(petName, id);
    }

//    @RequestMapping("queryattentionuser")
//    public ModelAndView queryAttentionUser(@RequestParam("id")String id){
//        ModelAndView modelAndView = new ModelAndView();
//        //查询用户的全部关注 废弃
//        List<Integer> topicId = userFunctionService.queryAttentionUser(id);
//        int a = topicId.size();//关注数
//        System.out.println("用户关注了"+a+"个话题");
//        List<Topic> topics = userFunctionService.queryAttentiontopicName(topicId);
//        modelAndView.addObject("topiclist",topics);
//        modelAndView.setViewName("myattention");
//
//        for(Topic t:topics)System.out.println(t);
//        return modelAndView;
//    }
    @RequestMapping("queryAttentiontopicName")
    public ModelAndView queryAttentiontopicName(HttpSession session){
        //查询用户关注的全部话题
        ModelAndView modelAndView = new ModelAndView();
        String id = (String)session.getAttribute("userId");
        List<Topic> topics = userFunctionService.queryAttentiontopicName(id);

        for(Topic t:topics)System.out.println(t);
        modelAndView.addObject("topics",topics);
        modelAndView.addObject("userid",id);
        modelAndView.setViewName("myattention02");


        return modelAndView;
    }

    @RequestMapping("queryTopic")
    public ModelAndView queryTopic(@RequestParam("id")long id) {
        //查询单个话题 显示话题内的帖子 ok
        Topic topic = userFunctionService.queryTopic(id);
        long day = dateUtil.getUserday(topic.getTopicDate());
        System.out.println("话题名" + topic.getTopicName());

        for (Posts posts : topic.getPosts()) {
            System.out.println(posts);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("topic",topic);
        modelAndView.addObject("day",day);
        modelAndView.addObject("posts",topic.getPosts());
        modelAndView.setViewName("topic");

        return modelAndView;
    }

    @RequestMapping("queryposts")
    public ModelAndView queryPosts(@RequestParam("p")long postId){
        //查询帖子全部信息 ok
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----帖子");
        Posts posts = userFunctionService.queryPost(postId);

        System.out.println(posts);
        modelAndView.addObject("post",posts);//帖子
        modelAndView.addObject("user",posts.getUsers());//发帖人
        modelAndView.setViewName("post");
        return modelAndView;
    }



    @RequestMapping("queryuserCollect")
    public ModelAndView queryUserCollect(HttpSession session){

        //查询用户收藏的全部话题  ok
        System.out.println("我的收藏");
        String id = (String)session.getAttribute("userId");
        List<Posts> posts = userFunctionService.queryUserCollect(id);
        System.out.println(posts);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("posts",posts);
        modelAndView.addObject("id",id);
        modelAndView.setViewName("mycollect");

        return modelAndView;
}

    @RequestMapping("querymyposts")
    public ModelAndView querymyposts(HttpSession session){
        System.out.println("我的帖子");
        String id = (String)session.getAttribute("userId");
        ModelAndView modelAndView = new ModelAndView();
        List<Posts> posts = userFunctionService.querymyposts(id);
        System.out.println(posts);
        modelAndView.addObject("posts",posts);
        modelAndView.setViewName("myposts");

        return modelAndView;
    }

    @RequestMapping("querymycommment")
    public ModelAndView querymycomment(HttpSession session){
        System.out.println("我的评论");
        String id = (String)session.getAttribute("userId");
        ModelAndView modelAndView = new ModelAndView();
        List  comments=userFunctionService.querymycomment(id);
        System.out.println(comments);
//        List<Reply> replies = userFunctionService.queryReply(id);
        modelAndView.addObject("comment",comments);
//        modelAndView.addObject("reply",replies);
        modelAndView.setViewName("mycomment");
        return modelAndView;
    }

    @RequestMapping("queryothersdata")
    public ModelAndView queryothersdata(@RequestParam("othersName")String otherName,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();

        User u = services.getUserbyidother(otherName);
        System.out.println(u);
        String othersid = aesUtil.idencrypt(u.getUserId()+"");
        long day = dateUtil.getUserday(u.getUserDate());//转换日期为注册天数
        session.setAttribute("othersid",othersid);
        modelAndView.addObject("user",u);
        modelAndView.addObject("day",day);
        modelAndView.setViewName("othersdata");

        return modelAndView;

    }

//    @RequestMapping("queryotherAttention")
//    public ModelAndView queryotherAttention(@RequestParam("otherid")long otherid){
//
//
//    }

}
